package net.oschina.app.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import net.oschina.app.Myapp;
import net.oschina.app.R;
import net.oschina.app.base.BaseFragment;
import net.oschina.app.hjpbean.BlogBean;
import net.oschina.app.hjpbean.GeneralBlogBean;
import net.oschina.app.ui.ZiXunActivity;
import net.oschina.app.util.BaseRecyclerAdapter;
import net.oschina.app.util.RecyclerViewHolder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Admin on 2017/7/16.
 */

public class ZongHeTJFangment extends BaseFragment {

    private List<BlogBean> newsLists;

    @BindView(R.id.rcv_news_zixun)
    RecyclerView rcvNewsZixun;
    @BindView(R.id.swipe_refresh_widget)
    SwipeRefreshLayout swipeRefreshWidget;
    Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View zonghetjfangment = inflater.inflate(R.layout.fragment_default, container, false);

        unbinder = ButterKnife.bind(this, zonghetjfangment);

        return zonghetjfangment;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        getshuju();

        Log.d("baima",123+"");

    }

    public void getshuju() {

        retrofit2.Call<GeneralBlogBean> info = Myapp.shopService.getRecommendInfo(0,20,"recommend");

        info.enqueue(new Callback<GeneralBlogBean>() {
            @Override
            public void onResponse(retrofit2.Call<GeneralBlogBean> call, Response<GeneralBlogBean> response) {

                newsLists= response.body().getBlogs();

                Log.d("baima", newsLists.get(15).getDocumentType()+"");

                initView();

            }

            @Override
            public void onFailure(retrofit2.Call<GeneralBlogBean> call, Throwable t) {

                t.printStackTrace();

            }
        });

    }

    private void initView() {

        rcvNewsZixun.setLayoutManager(new LinearLayoutManager(getContext()));


//        LinearLayoutManager manager = new LinearLayoutManager(getContext());
//        zongheZixunRcv.setLayoutManager(manager);

        RecyclerView.Adapter adapter=new BaseRecyclerAdapter<BlogBean>(getContext(),newsLists) {
            @Override
            public int getItemLayoutId(int viewType) {

                return R.layout.zonghe_rcv_item;

            }

            @Override
            public void bindData(RecyclerViewHolder holder, int position, BlogBean item) {

                Log.d("baima","到此一游");
                if(newsLists.get(position).getDocumentType()==1){
                    holder.getImageView(R.id.zonghe_item_iv).setImageResource(R.drawable.widget_original_icon);
                }else if(newsLists.get(position).getDocumentType()==0){
                    holder.getImageView(R.id.zonghe_item_iv).setImageResource(R.drawable.widget_repaste_icon);
                }
                holder.getTextView(R.id.zonghe_zixunrcv_tvtitle).setText(newsLists.get(position).getTitle());
                holder.getTextView(R.id.zonghe_zixunrcv_tvneirong).setText("        "+newsLists.get(position).getBody());
                holder.getTextView(R.id.zonghe_zixunrcv_yonghuming).setText(newsLists.get(position).getAuthorname());
                holder.getTextView(R.id.zonghe_zixunrcv_tvtime).setText(newsLists.get(position).getPubDate());
                holder.getTextView(R.id.zonghe_zixunrcv_tvpinglun).setText(newsLists.get(position).getCommentCount()+"");


            }

        };

        rcvNewsZixun.setAdapter(adapter);

        ((BaseRecyclerAdapter)adapter).setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int pos) {

//                Toast.makeText(getContext(),newsLists.get(1).getTitle(),Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(getActivity(),BoKeActivity.class);
                intent.putExtra("title","推荐详情");
                intent.putExtra("id",newsLists.get(pos).getId());
                startActivity(intent);

            }
        });


    }

}
